package com.site.user.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.site.data.QueryResult;
import com.site.user.pojo.TbResource;
import com.site.user.pojo.TbUser;
import com.site.user.service.ITbResourceService;

@Controller
@RequestMapping("/resource")
public class TbResourceController extends BaseController {
	@Resource
	private ITbResourceService tbResourceService;
	
	/**
	 * 返回资源树型数据
	 * @param request
	 * @param response
	 * @param out
	 */
	@RequestMapping(value = "/ser_getResTree", method = RequestMethod.POST)
	public void getResTree(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setHeader("Content-Type", "text/html;charset=utf-8");


		String id = ServletRequestUtils.getStringParameter(request, "id", "");
		String idResource = ServletRequestUtils.getStringParameter(request, "idResource", "");		
		String treedata=tbResourceService.getResTree(id,idResource);
		

		out.println(treedata);

	}
	/**
	 * @param request
	 * @param response
	 * @param out
	 * @param entity
	 */
	@RequestMapping(value = "/ser_saveItem", method = RequestMethod.POST)
	public void saveItem(HttpServletRequest request, HttpServletResponse response, PrintWriter out, TbResource entity) {
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setHeader("Content-Type", "text/html;charset=utf-8");
		String msg = "";		
		TbUser user = GetLoginSession(request, response);
		if (null != user) {			
			String id = UUID.randomUUID().toString().replace("-", "");
			if(StringUtils.isEmpty(entity.getIdResource())){
				entity.setIdResource(id);
				tbResourceService.execSave(entity);
			}else{
				tbResourceService.execUpdate(entity);
			}
			//JSONObject jsob=new JSONObject(entity);			
			//msg = "id:'"+id+"',text:'"+entity.getResName()+"',attributes:"+jsob.toString();
			msg="保存成功！";
		} else {
			msg = "会话过期,请点击右上角的注销重新登录.";
		}
		out.println(msg);
	}
	
	/**
	 * 保存系统数据
	 * @param request
	 * @param response
	 * @param out
	 */
	@RequestMapping(value = "/ser_save", method = RequestMethod.POST)
	public void save(HttpServletRequest request, HttpServletResponse response, PrintWriter out, TbResource entity) {
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setHeader("Content-Type", "text/html;charset=utf-8");
		String msg = "";
		String id = UUID.randomUUID().toString().replace("-", "");
		entity.setIdResource(id);
		entity.setSign("1");
		entity.setParentId("-1");
		entity.setResType("1");
		entity.setLevel(0);
		entity.setSystemId("-1");
		TbUser user = GetLoginSession(request, response);
		if (null != user) {
			
			tbResourceService.execSave(entity);
			msg = "保存成功.";
		} else {
			msg = "会话过期,请点击右上角的注销重新登录.";
		}
		out.println(msg);
	}

	/**
	 * 查询数据
	 * @param request
	 * @param response
	 * @param out
	 */
	@RequestMapping(value = "/ser_search", method = RequestMethod.POST)
	public void search(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setHeader("Content-Type", "text/html;charset=utf-8");

		String whereJPQL = "resType=1";

		Map mpparam = new HashMap();

		String resName = ServletRequestUtils.getStringParameter(request, "resName", "");
		if (StringUtils.isNotEmpty(resName)) {
			whereJPQL += " and resName like :resName";
			mpparam.put("resName", "%" + resName + "%");
		}
		
	

		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("orderby", "DESC");

		int pagesize = ServletRequestUtils.getIntParameter(request, "pagesize", 10);
		int page = ServletRequestUtils.getIntParameter(request, "page", 1);
		int maxResult = pagesize * page;
		int startIndex = pagesize * (page - 1);

		QueryResult result = tbResourceService.queryData(startIndex, maxResult, whereJPQL, mpparam, orderby);

		out.println(result.getResult("json"));

	}

	/**
	 * 载入数据
	 * @param request
	 * @param response
	 * @param out
	 */

	@RequestMapping(value = "/ser_load", method = RequestMethod.GET)
	public void load(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setHeader("Content-Type", "text/html;charset=utf-8");

		String id = ServletRequestUtils.getStringParameter(request, "id", "");
		TbResource obj = null;

		if (StringUtils.isNotEmpty(id)) {

			obj = tbResourceService.execFindById(id);
			if (obj != null) {
				JSONObject jobj = new JSONObject(obj);
				out.println(jobj.toString());
			}
		} else {
			out.println("");
		}

	}

	/**
	 * 删除数据
	 * @param request
	 * @param response
	 * @param out
	 */
	@RequestMapping(value = "/ser_delete", method = RequestMethod.POST)
	public void delete(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setHeader("Content-Type", "text/html;charset=utf-8");
		String msg = "";
		try {
			String ids = ServletRequestUtils.getStringParameter(request, "ids", "");
			if (StringUtils.isNotEmpty(ids)) {
				String wheresql = " o.idResource in ('" + ids + "')";
				tbResourceService.queryDeleteBatch(wheresql);
				msg = "删除成功!";
			}
		} catch (Exception e) {
			msg = "删除数据出现异常!" + e.getMessage();
		}

		out.println(msg);
	}

	/**
	 * 修改数据
	 * @param request
	 * @param response
	 * @param out
	 */
	@RequestMapping(value = "/ser_update", method = RequestMethod.POST)
	public void update(HttpServletRequest request, HttpServletResponse response, PrintWriter out, TbResource entity) {
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setHeader("Content-Type", "text/html;charset=utf-8");
		
		String id = ServletRequestUtils.getStringParameter(request, "idResource","");
		String msg="";
		TbResource obj=tbResourceService.execFindById(id);
		if(obj!=null){
			String resName = ServletRequestUtils.getStringParameter(request, "resName", "");
			if (StringUtils.isNotEmpty(resName)) {
				obj.setResName(resName);
			}			
			String resource = ServletRequestUtils.getStringParameter(request, "resource", "");
			if (StringUtils.isNotEmpty(resource)) {
				obj.setResource(resource);
			}			
			int orderby = ServletRequestUtils.getIntParameter(request, "orderby", 0);
			obj.setOrderby(orderby);
			String resDesc = ServletRequestUtils.getStringParameter(request, "resDesc", "");
			if (StringUtils.isNotEmpty(resDesc)) {
				obj.setResDesc(resDesc);
			}
			tbResourceService.execUpdate(obj);
			msg="记录更新成功！";
		}else{
			msg="数据加载有异常，请确认传入的Id是否正确？";
		}
		out.println(msg);


	}
}
